package com.tian.demo01.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tian.demo01.dao.EmpMapper;
import com.tian.demo01.entity.Emp;
import com.tian.demo01.service.EmpService;
import com.tian.demo01.vo.EmpVo;
import com.tian.demo01.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author: Tian
 * @version: 1.0
 * @since: 1.0
 * @date: 2024/7/14/11:45
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    EmpMapper empMapper;

    @Override
    public R selectAll(EmpVo empVo) {
        IPage<Emp> page = new Page<>(empVo.getCurrent(),empVo.getSize());
        QueryWrapper<Emp> wrapper=new QueryWrapper();
        if(!StringUtils.isEmpty(empVo.getEmpno())){
            wrapper.eq("empno",empVo.getEmpno());
        }
        if(!StringUtils.isEmpty(empVo.getDeptno())){
            wrapper.eq("deptno",empVo.getDeptno());
        }
        if(!StringUtils.isEmpty(empVo.getEname())){
            wrapper.like("ename",empVo.getEname());
        }
        List<Emp> emps = empMapper.selectEmp(page, wrapper);
        page.setRecords(emps);
        if(emps==null){
            return new R(404,"没有找到符合的员工信息",emps);
        }
        return new R(200,"查询成功",page);
    }

    @Override
    public R insertEmp(Emp emp) {
        int i = empMapper.insert(emp);
        if(i==0){
            return new R(500,"添加失败",i);
        }
        return new R(200,"添加员工信息成功",i);
    }

    @Override
    public R updateById(Emp emp) {
        int i = empMapper.updateById(emp);
        if(i==0){
            return new R(500,"修改员工信息失败",i);
        }
        return new R(200,"修改员工信息成功",i);
    }

    @Override
    public R deleteById(int id) {
        int i = empMapper.deleteById(id);
        if (i==0){
            return new R(404,"该员工不存在",i);
        }
        return new R(200,"成功删除该员工信息",i);
    }
}
